package com.yxdj.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxdj.service.api.domain.MainCategory;
import com.yxdj.service.api.domain.ServiceItem;
import com.yxdj.service.api.domain.SubCategory;
import com.yxdj.service.mapper.MainCategoryMapper;
import com.yxdj.service.mapper.ServiceItemMapper;
import com.yxdj.service.mapper.SubCategoryMapper;
import com.yxdj.service.service.MainCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author 韩总
 * @description 针对表【main_category(主分类表)】的数据库操作Service实现
 * @createDate 2025-03-20 14:44:40
 */
@Service
public class MainCategoryServiceImpl extends ServiceImpl<MainCategoryMapper, MainCategory>
        implements MainCategoryService {

    @Autowired
    private SubCategoryMapper subCategoryMapper;

    @Autowired
    private ServiceItemMapper serviceItemMapper;

    @Override
    public List<MainCategory> getMainCategoryList(Integer limit) {
        return baseMapper.getMainCategoryList(limit);
    }

    @Override
    public List<MainCategory> getAllCategory() {
        List<MainCategory> mainCategories = baseMapper.selectList(null);
        mainCategories.forEach(mainCategory -> {
            List<SubCategory> subCategoryList = subCategoryMapper.selectList(new LambdaQueryWrapper<SubCategory>().eq(SubCategory::getMainCategoryId, mainCategory.getId()));
            if (!CollectionUtils.isEmpty(subCategoryList)) {
                subCategoryList.forEach(subCategory -> {
                    List<ServiceItem> serviceItemList = serviceItemMapper.selectList(new LambdaQueryWrapper<ServiceItem>().eq(ServiceItem::getSubCategoryId, subCategory.getId()));
                    if (!CollectionUtils.isEmpty(serviceItemList)) {
                        subCategory.setServiceItemList(serviceItemList);
                    }
                });
                mainCategory.setSubCategoryList(subCategoryList);
            }
        });
        return mainCategories;
    }
}




